package com.dawn.module.system.dal.mysql.logger;

import com.dawn.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.dawn.framework.common.pojo.PageResult;
import com.dawn.framework.mybatis.core.mapper.BaseMapperX;
import com.dawn.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.dawn.module.system.controller.admin.dict.vo.data.DictDataPageReqVO;
import com.dawn.module.system.controller.admin.logger.vo.operatelog.OperateLogPageReqVO;
import com.dawn.module.system.dal.dataobject.dict.DictDataDO;
import com.dawn.module.system.dal.dataobject.logger.OperateLogDO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collection;

@Mapper
public interface OperateLogMapper extends BaseMapperX<OperateLogDO> {

    default PageResult<OperateLogDO> selectPage(OperateLogPageReqVO reqVO) {
        LambdaQueryWrapperX<OperateLogDO> query = new LambdaQueryWrapperX<OperateLogDO>()
                .likeIfPresent(OperateLogDO::getModule, reqVO.getModule())
                .likeIfPresent(OperateLogDO::getName, reqVO.getName())
                .likeIfPresent(OperateLogDO::getUserNickname, reqVO.getUserNickname())
                .likeIfPresent(OperateLogDO::getRequestUrl, reqVO.getRequestUrl())
                .likeIfPresent(OperateLogDO::getJavaMethodArgs, reqVO.getJavaMethodArgs())
                .likeIfPresent(OperateLogDO::getResultMsg, reqVO.getResultMsg())
                .likeIfPresent(OperateLogDO::getResultData, reqVO.getResultData())
                .eqIfPresent(OperateLogDO::getType, reqVO.getType())
                .orderByDesc(OperateLogDO::getId);
        if (Boolean.TRUE.equals(reqVO.getSuccess())) {
            query.eq(OperateLogDO::getResultCode, GlobalErrorCodeConstants.SUCCESS.getCode());
        } else if (Boolean.FALSE.equals(reqVO.getSuccess())) {
            query.gt(OperateLogDO::getResultCode, GlobalErrorCodeConstants.SUCCESS.getCode());
        }
        query.orderByDesc(OperateLogDO::getId); // 降序
        return selectPage(reqVO, query);
    }

    default PageResult<OperateLogDO> selectPage1(OperateLogPageReqVO reqVO, Collection<Long> userIds) {
        LambdaQueryWrapperX<OperateLogDO> query = new LambdaQueryWrapperX<OperateLogDO>()
                .likeIfPresent(OperateLogDO::getModule, reqVO.getModule())
                .inIfPresent(OperateLogDO::getUserId, userIds)
                .eqIfPresent(OperateLogDO::getType, reqVO.getType())
                .betweenIfPresent(OperateLogDO::getStartTime, reqVO.getStartTime());
        if (Boolean.TRUE.equals(reqVO.getSuccess())) {
            query.eq(OperateLogDO::getResultCode, GlobalErrorCodeConstants.SUCCESS.getCode());
        } else if (Boolean.FALSE.equals(reqVO.getSuccess())) {
            query.gt(OperateLogDO::getResultCode, GlobalErrorCodeConstants.SUCCESS.getCode());
        }
        query.orderByDesc(OperateLogDO::getId); // 降序
        return selectPage(reqVO, query);
    }

    /**
     * 物理删除指定时间之前的日志
     *
     * @param createTime 最大时间
     * @param limit 删除条数，防止一次删除太多
     * @return 删除条数
     */
    @Delete("DELETE FROM system_operate_log WHERE create_time < #{createTime} LIMIT #{limit}")
    Integer deleteByCreateTimeLt(@Param("createTime") LocalDateTime createTime, @Param("limit") Integer limit);

}
